package com.wnxy.sddfp.system.controller;

import cn.hutool.core.util.RandomUtil;
import com.wnxy.sddfp.client.bank.api.TradeLogApi;
import com.wnxy.sddfp.common.model.dto.bank.CardBindDto;
import com.wnxy.sddfp.common.model.dto.bank.SignatureDto;
import com.wnxy.sddfp.common.model.dto.user.LoginDto;
import com.wnxy.sddfp.common.model.dto.user.UserRegisterDto;
import com.wnxy.sddfp.common.model.result.Result;
import com.wnxy.sddfp.common.model.vo.LoginVo;
import com.wnxy.sddfp.common.model.vo.UserInfoVo;
import com.wnxy.sddfp.system.service.IUserService;
import com.wnxy.sddfp.system.utils.TokenUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author yellow docker
 * @since 2024-07-29
 */
@RestController
@RequestMapping("/user")
public class UserController {
    private final IUserService userService;

    public UserController(IUserService userService) {
        this.userService = userService;
    }


    /**
     * 根据用户id查询
     */
    @ApiOperation("根据用户id查询")
    @GetMapping("/inner/findById/{userId}")
    public Result<UserInfoVo> findById(@PathVariable("userId") Long userId) {
        UserInfoVo userInfoVo = userService.findById(userId);
        return Result.ok(userInfoVo);
    }

    @GetMapping("/testToken")
    public String list(HttpServletRequest request) {
        String userId = request.getHeader("userId");
        System.out.println("userId = " + userId);

        String headerUserId = TokenUtils.getHeaderUserId();
        System.out.println("headerUserId = " + headerUserId);

        return "testToken";
    }

    /**
     * 用户绑卡
     */
    @PostMapping("/bank/card/add")
    public Result<SignatureDto> addBankAccount(@RequestBody CardBindDto cardBindDto) {
        SignatureDto signatureDto = userService.addBankAccount(cardBindDto);

        return Result.ok(signatureDto);
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = userService.login(loginDto);
        return Result.ok(loginVo);
    }


    /**
     * 用户注册
     *
     * @Valid 启用参数校验
     * 1、UserRegisterDto 的工具的封装
     * 2、Result的结果的统一返回
     * 3、@Valid + 全局参数异常处理
     */
    @PostMapping("/regist")
    public Result regist(@RequestBody @Valid UserRegisterDto userRegistDto) {
        userService.register(userRegistDto);
        return Result.ok();
    }

    @Autowired
    private TradeLogApi tradeLogApi;

    @GetMapping("/user/{id}")
    public Result testOpenfeign(@PathVariable("id") Integer id) {

        Result result = tradeLogApi.test01(id);

        return result;
    }

    @Value("${server.port}")
    private Integer port;

    @GetMapping("/testFeign/{id}")
    public String testFeign(@PathVariable("id") Integer id) {

        String code = RandomUtil.randomNumbers(6);

        return code + ":" + id + ":" + port;
    }


    // @GetMapping("/feign/{id}")
    // @HystrixCommand(fallbackMethod = "errorFallback", commandProperties = {
    //         // 是否开启断路器
    //         @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ENABLED, value = "true"),
    //         // 请求错误率大于 50% 就启动熔断器
    //         @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ERROR_THRESHOLD_PERCENTAGE, value = "50"),
    //         // 默认10秒内访问接口达到20次，且错误率大于50%则打开断路器，触发熔断； 为了方便测试，这里修改请求数为10
    //         @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_REQUEST_VOLUME_THRESHOLD, value = "10"),
    //         // 断路多久以后开始尝试是否恢复，默认5s
    //         @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS, value = "10000")
    // })
    // public Result testOpenfeign(@PathVariable("id") Integer id) {
    //     // Result result = tradeLogApi.test01(id);
    //     if (id < 10) {
    //         throw new BusinessException();
    //     }
    //
    //     return Result.ok();
    // }
    //
    // // 降级方法
    // public Result errorFallback(@PathVariable("id") Integer id) {
    //     return Result.fail("降级方法");
    // }


}
